package com.xiyu.service.repository.restaurant.dish;

import com.xiyu.service.model.restaurant.dish.RestaurantDishType;
import com.xiyu.service.model.restaurant.dish.RestaurantDishTypeTable;
import com.xiyu.service.vo.restaurant.dish.DishPageQueryDishTypeInput;
import org.babyfish.jimmer.spring.repository.JRepository;
import org.babyfish.jimmer.Page;
import org.springframework.util.StringUtils;

import java.util.List;
import java.util.Optional;

public interface RestaurantDishTypeRepository extends JRepository<RestaurantDishType, Long> {
    RestaurantDishTypeTable restaurantDishTypeTable = RestaurantDishTypeTable.$;

    default Page<RestaurantDishType> pageQuery(DishPageQueryDishTypeInput inputVO){
        return sql().createQuery(restaurantDishTypeTable)
                .whereIf(StringUtils.hasText(inputVO.getName()), () -> restaurantDishTypeTable.name().like(inputVO.getName()))
                .orderBy(restaurantDishTypeTable.id().desc())
                .select(restaurantDishTypeTable)
                .fetchPage(inputVO.getPageNo() - 1, inputVO.getPageSize());
    }

    Optional<RestaurantDishType> findFirstByName(String name);

    Optional<RestaurantDishType> findFirstByNameAndIdNot(String name, Long id);

    default List<RestaurantDishType> listQueryDishType(){
        return sql().createQuery(restaurantDishTypeTable)
                .orderBy(restaurantDishTypeTable.id().desc())
                .select(restaurantDishTypeTable)
                .execute();
    }


}